package tjs.study.notes.eureka.clienta.user.service.feign.exception;

import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;
import tjs.study.notes.eureka.clienta.user.service.feign.OrederFeignApi;

/**
 * @program: tjs-spcloud
 * @desc: 不只重写了方法，还能捕获异常
 * @author: tjs
 * @create: 2019-10-05 23:22
 */
@Component  //这个注解不能忘
public class FeignConfigFallbackFactory implements FallbackFactory<OrederFeignApi> {

    @Override
    public OrederFeignApi create(Throwable cause) {

        return new OrederFeignApi() {
            @Override
            public String newOrder(String userId, Integer sleepTime) {
                // 这里可以根据错误信息判断是否要return，
                // 还是直接往上抛出异常，理论上fallBack都不应该抛出异常，fallBack的目的是为了缓解服务器压力，而非抛出异常回滚
                return "feign-sorry " + userId + cause.getMessage();
            }
        };
    }
}
